home *** CD-ROM | disk | FTP | other *** search
/ Aminet 45 / Aminet 45 (2001)(GTI - Schatztruhe)[!][Oct 2001].iso / Aminet / game / role / ldmud-3.2-bin.lha / mud / doc / man2tex.awk (.txt) < prev    next >
LaTeX Document  |  1999-03-31  |  4KB  |  108 lines

  1. #! /usr/local/bin/gawk -f
  2. # a simple example script to convert a manpage into a LaTeX section
  3. # ToDo:
  4. # - preserve line breaks and indentation in Examples
  5. # - generate index entries for the section names
  6. # - generate cross references for the See Also entries.
  7. ## The behaviour of GNU awk was changed somewhere between versions 2.15.0
  8. ## and 2.15.5, regarding the backslash and & in the gsub() invocations
  9. ## below. If you get ampersands instead of backslash-underscores in
  10. ## the output, you need to experiment with the second arg of the gsub()s,
  11. ## maybe add two more backslashes (making 6 in total)
  12. ## Pepel sighs deeply
  13. # firts, substitute electrical characters
  14.         { gsub(/\\/,"$\\backslash$");
  15.           gsub(/[_{}%&\#\^]/, "\\\\&");
  16.           gsub(/[<>]/,"$&$");
  17. ##          gsub("\"","\\quote+\"+");
  18. #          gsub("\{", "\\{");
  19. #          gsub("\}", "\\}");
  20. #          print $0;
  21. /^NAME|^CONCEPT/    { state = "name";
  22.               # getline line;
  23.               # split(line, name, "[ \t]+");
  24.               # gsub(/[_{}%&]/, "\\\\&", name[1]);
  25.               # printf("\\section{%s\\label{%s-section}}\n",
  26.               #       name[1], name[1]);
  27.               # print line "\\\\";
  28.               next;
  29. /^SYNOPSIS/    { state = "syn";
  30.           # getline line; name = line;
  31. #          ## gsub(/[ \t]+/," ",name);
  32. #          ## print l;
  33.           # sub(/^[ \t]*[a-z]+[ \t]+\**/,"", name);
  34.           # sub(/\([^)]*\)/,"()", name);
  35.           # sub(/\).*$/,")", name);
  36.           # gsub(/[_{}%&]/, "\\\\&", name);
  37.           # gsub(/[_{}%&]/, "\\\\&", line);
  38. #          # print name;
  39.           # printf("\\section{%s\\label{%s-section}}\n", name, name);
  40.           # print line "\\\\"
  41.           next;
  42. /^LAST UPDATE/    { state = "X"; print "\\subsection*{Last Update}\n"; next; }
  43. /^SEE ALSO/    { state = "see"; print "\\subsection*{See Also}\n"; next; }
  44. /^EXAMPLE/    { state = "exm"; # EXAMPLE or EXAMPLES
  45.           printf("\\subsection*{%s%s}\n",
  46.              toupper(substr($1, 1, 1)),
  47.              tolower(substr($1, 2)));
  48.           next;
  49.         }        
  50. /^[A-Z]+/    { state = "X";
  51.           printf("\\subsection*{%s%s}\n",
  52.              toupper(substr($1, 1, 1)),
  53.              tolower(substr($1, 2)));
  54.           next;
  55. # a match-all , again
  56.         { if ("X" == state) { print $0; next; }
  57.           if ("name" == state) {
  58.             split($0, namename, "[ \t]+");
  59.             # gsub(/[_{}%&]/, "\\\\&", namename[1]);
  60.             label = namename[1];
  61.             if ("" == label) label = $0;
  62.             gsub(/[_(){}%&^~\#\\\\]/, "", label);
  63.             printf("\\section{%s\\label{%s-section}}\n%s\\\\\n",
  64.                $0, label, line);
  65.             next;
  66.           }
  67.           if ("see" == state) {
  68.             # print $0;
  69.             gsub(/[     ]/, "", $0);
  70.             nrefs = split($0, refs, ",");
  71.             for (i = 1; i < nrefs; i++) {
  72.               if (!length(refs[i])) continue;
  73.               seename = substr(refs[i], 1, index(refs[i], "(")-1);
  74.               printf("%s", refs[i]);
  75.               gsub(/[_(){}%&^~\\\\]/, "", seename);
  76.               if (length(seename))
  77.              printf(" (\\ref{%s-section})", seename);
  78.               printf(", ");
  79.             }
  80.             print "";            
  81.             next;
  82.           }
  83.           if ("syn" == state) {
  84.             syname = $0;
  85.             sub(/^[ \t]*[a-z]+[ \t]+\**/,"", syname);
  86.             sub(/\([^)]*\)/,"()", syname);
  87.             sub(/\).*$/,")", syname);
  88. #            # print syname;
  89.             label = syname;
  90.             gsub(/[_(){}%&^~\\\\]/, "", label);
  91.             printf("\\section{{\\tt %s}\\label{%s-section}}\n",
  92.                syname, label);
  93.             printf("~~{\\tt %s}\\\\\n", $0);
  94.             state = "syn2";
  95.             next;
  96.           }
  97.           if ("syn2" == state) {
  98.             printf("~~{\\tt %s}\\\\\n", $0);
  99.             next;
  100.           }
  101. # print the entire examples section in \tt and lines separated.
  102. # we need some heuristic to distinguish betwwen code and explanatory lines
  103.           if ("exm" == state) {
  104.             printf("{\\tt %s}\\\\\n", $0);
  105.             next;
  106.           }
  107.           print "unknown state " state > /dev/stderr;
  108.